package com.rules.admin.controller;

import com.rules.admin.utils.ResultUtils;
import com.rules.admin.entity.UnitCase;
import com.rules.admin.service.UnitCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping({"/unitcases"})
public class UnitCaseController {
   @Autowired
   private UnitCaseService unitCaseService;

   @RequestMapping(
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> create(@RequestBody UnitCase unitCase) {
      this.unitCaseService.save(unitCase);
      return ResultUtils.getSuccessResultData(unitCase);
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> delete(@PathVariable("id") Long id) {
      boolean result = this.unitCaseService.removeById(id);
      return result ? ResultUtils.getSuccessResultData() : ResultUtils.getFaildResultData(new String[]{"删除失败"});
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> update(@RequestBody UnitCase unitCase) {
      this.unitCaseService.save(unitCase);
      return ResultUtils.getSuccessResultData(unitCase);
   }

   @RequestMapping(
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public List<UnitCase> find(String groupId, String artifactId) {
      return this.unitCaseService.selectUnitCasesByPackage(groupId, artifactId);
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> get(@PathVariable("id") Long id) {
      return ResultUtils.getSuccessResultData(this.unitCaseService.getById(id));
   }
}
